package com.xingyi.bacs.controller;

import com.xingyi.bacs.model.Rerun;
import com.xingyi.bacs.util.DatetimeUtil;
import com.xingyi.bacs.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;

/**
 * Created by wujt on 13/10/2021
 */
@Controller
public class IndexController {

    @Resource
    private Rerun rerun;

    @RequestMapping("/")
    public String homePage(Model model) {
        System.out.println(rerun);
        model.addAttribute("rerun", rerun);
        return "index";
    }

    @PostMapping("/submit")
    public String getSubmit(String newURL, Model model) {
        String yesterday = DatetimeUtil.getYesterday(DatetimeUtil.DATE_FORMAT);
        if (StringUtil.hasValue(newURL)){
            if (rerun.getQs_Bill().equals(newURL)){
                return "forward:"+newURL;
            }else{
                return "forward:"+newURL+"?date="+yesterday;
            }
        }else{
            model.addAttribute("returnData","数据表不存在");
            return "index";
        }
    }

    @RequestMapping("/excel")
    public String excelPage(Model model) {
        return "excel";
    }
}
